/**
 * LVL.Components - Component utilities.
 */

define(['blacksmith/game'], function() {


LVL.Components = {}



LVL.Components.registered = {}



/**
 * Registers an LVL.Component.
 * 
 * @param {string} name
 * @param {function} constructor
 * @param {array} parents
 * @param {function} createImage
 */
LVL.Components.register = function(name, constructor, parents, createImage) {
    
    var parent
    
    if (!LVL.Components.registered.hasOwnProperty(name))
        LVL.Components.registered[name] = {}
    
    LVL.Components.registered[name].constructor = constructor
    LVL.Components.registered[name].createImage = createImage
    LVL.Components.registered[name].children = {}
    
    for (parent in parents) {
        
        parent = parents[parent]
        
        if (!LVL.Components.registered.hasOwnProperty(parent))
            LVL.Components.registered[parent] = {children:{}}
        
        LVL.Components.registered[parent].children[name] = null
        
    }
    
    
}



/**
 * Creates images for all registered components.
 */
LVL.Components.createImages = function() {
    
    var c
    
    GAME.sun.target.rotation.set(0.5, 0.2, 0.5)
    
    GAME.Renderer.domElement.style.width = '64px'
    GAME.Renderer.domElement.style.height = '64px'
    
    for (c in LVL.Components.registered)
        if (LVL.Components.registered[c].hasOwnProperty('createImage'))
            LVL.Components.registered[c].image = LVL.Components.registered[c].createImage()
    
    GAME.Renderer.domElement.style.width = '100%'
    GAME.Renderer.domElement.style.height = '100%'
    
    GAME.Camera.object.position.set(0, 1500, 900)
    GAME.panUp(-65)
    
}




/**
 * Returns named components image.
 * 
 * @return image data
 */
LVL.Components.getImage = function(name) {
    
    if (LVL.Components.registered.hasOwnProperty(name))
        return LVL.Components.registered[name].image
    
}



})
